find_program(PANDOC pandoc) # terminal command `pandoc` or `pandoc-cli`

if (PANDOC)
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tv.1
    COMMAND ${PANDOC} ARGS "${CMAKE_CURRENT_SOURCE_DIR}/tv.md" "-s" "-t" "man" "-o" "${CMAKE_CURRENT_BINARY_DIR}/tv.1"
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tv.md #deps files
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
    COMMENT "Building manpage tv.1, and check by man -l tv.1"
    ) 

    add_custom_target(man ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tv.1)

    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tv.1
    DESTINATION ${CMAKE_INSTALL_FULL_MANDIR}/man1
    )

else()
    message(WARNING "Not re-generate manpage, Make sure pandoc installed")
    install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/tv.1
    DESTINATION ${CMAKE_INSTALL_FULL_MANDIR}/man1 # system man dir
    )
endif()